import {fetchConfig} from "~/composables/useHttp.js";

/**
 * @description 登录
 * @param {String} username
 * @param {String} password
 * @returns
 */
export const useLoingApi = (body) => {
    return useHttpPost('login', '/login', {
        body
    })
}


/**
 * @description 注册
 * @param {String} username
 * @param {String} password
 * @returns
 */
export const useRegApi = (body) => {
    return useHttpPost('reg', '/reg', {
        body
    })
}

/**
 * @description 获取用户信息
 * @returns
 */

export const useGetinfoApi = () => {
    return useHttpGet('getinfo', '/getinfo', {
        $: true
    })
}


/**
 * @description 退出登录
 * @returns
 */

export const useLogoutApi = () => {
    return useHttpPost("logout", '/logout',)
}


/**
 * @description 发送验证码
 * @param {String} phone 手机号
 */

export const useGetCaptchaApi = (phone) => {
    return useHttpPost('getCaptcha', '/get_captcha', {
        body: {
            phone
        }
    })
}


/**
 * @description 绑定手机号
 * @param { String } phone 手机号
 * @param { String } code 验证码
 */

export const useBindPhoneApi = (body) => {
    return useHttpPost('bindphone', '/bind_mobile', {
        body
    })
}


/**
 * @description 找回密码
 * @param { String } phone 手机号
 * @param { String } code 验证
 * @param { String } password 密码
 * @param { String } repassword 确认密码
 */

export const useForgetApi = (body) => {
    return useHttpPost('forget', '/forget', {
        body
    })
}


/**
 * @description  我的学习记录列表
 * @param { Number,String } page 页码
 * @param { String } type 类型：course课程，column专栏
 * @returns
 */

export function useUserHistoryApi(query) {
    return useHttpGet("userHistory", () => {
        const q = useQueryToString(query());
        return `/user_history/list${q}`
    }, {
        lazy: true
    })
}


/**
 * @description  获取购买记录
 * @param { Number,String } page 页码
 * @returns
 */

export function useOrderListApi(page) {
    return useHttpGet("orderList", `/order/list?page=${page}`, {
        lazy: true
    })
}


/**
 * @description  我的考试记录
 * @returns
 */
export function useUserTestApi(page) {
    return useHttpGet("UserTest", `/user_test/list?page=${page}`, {
        lazy: true
    })
}


/**
 * @description  我的帖子列表
 * @returns
 */
export function useMypostListApi(page) {
    return useHttpGet("MypostList", `/mypost?page=${page}`, {
        lazy: true
    })
}


/**
 * @description  获取优惠价记录
 * @param { Number,String } page 页码
 * @returns
 */

export function useUserCouponApi(page) {
    return useHttpGet("UserCoupon", `/user_coupon?page=${page}`, {
        lazy: true
    })
}


/**
 * @description  我的收藏列表
 * @param { Number,String } page 页码
 * @returns
 */

export function useMyFavaListApi(page) {
    return useHttpGet("MyFavaList", `/user_fava?page=${page}`, {
        lazy: true
    })
}


/**
 * @description  收藏
 * @param { Object } body 参数
 * @returns
 */

export const useCollectApi = (body) => {
    return useHttpPost('collect', '/collect', {
        body
    })
}


/**
 * @description  取消收藏
 * @param { Object } body 参数
 * @returns
 */

export const useUncollectApi = (body) => {
    return useHttpPost('Uncollect', '/uncollect', {
        body
    })
}


/**
 * @description  修改资料
 * @param { String } avatar 头像
 * @param { String } nickname 昵称
 * @param { String } sex 性别
 * @returns
 */

export const useUpdateUserInfoApi = (body) => {
    return useHttpPost('updateUserInfo', '/update_info', {
        body
    })
}


/**
 * @description  上传图片
 * @returns
 */

export const useUploadConfig = () => {
    const token = useCookie("token")
    return {
        action: fetchConfig.baseURL + "/upload",
        headers: {
            appid: fetchConfig.headers.appid,
            token: token.value,
        }
    }
}


/**
 * @description  修改密码
 * @returns
 */
export const useupdatePasswordApi = (body) => {
    return useHttpPost("updatePassword", "/update_password", {
        body
    });
}


/**
 * @description  领取优惠价
 * @returns
 */
export const useReceiveUserCouponApi = (body) => {
    return useHttpPost("receiveUserCoupon", "/user_coupon/receive", {
        body
    });
}